﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class TaiKhoanManager
    {
        public bool Add(TaiKhoan entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spTaiKhoanInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDTaiKhoan",
                                                                                             entity.IDTaiKhoan),
                                                                            new SqlParameter("@TenDangNhap",
                                                                                             entity.TenDangNhap),
                                                                            new SqlParameter("@MatKhau", entity.MatKhau)
                                                                            ,
                                                                            new SqlParameter("@IDLoaiTaiKhoan",
                                                                                             entity.IDLoaiTaiKhoan),
                                                                            new SqlParameter("@HoTen", entity.HoTen),
                                                                            new SqlParameter("@CMND", entity.CMND),
                                                                            new SqlParameter("@GioiTinh",
                                                                                             entity.GioiTinh),
                                                                            new SqlParameter("@NgaySinh",
                                                                                             entity.NgaySinh),
                                                                            new SqlParameter("@Email", entity.Email),
                                                                            new SqlParameter("@DienThoai",
                                                                                             entity.DienThoai),
                                                                            new SqlParameter("@IDTinhThanhPho",
                                                                                             entity.IDTinhThanhPho),
                                                                            new SqlParameter("@NgayDangKy",
                                                                                             entity.NgayDangKy),
                                                                            new SqlParameter("@NgayKetThuc",
                                                                                             entity.NgayKetThuc)
                                                                        }
                                                    });
        }

        public bool Update(TaiKhoan entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spTaiKhoanUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDTaiKhoan",
                                                                                             entity.IDTaiKhoan),
                                                                            new SqlParameter("@TenDangNhap",
                                                                                             entity.TenDangNhap),
                                                                            new SqlParameter("@MatKhau", entity.MatKhau)
                                                                            ,
                                                                            new SqlParameter("@IDLoaiTaiKhoan",
                                                                                             entity.IDLoaiTaiKhoan),
                                                                            new SqlParameter("@HoTen", entity.HoTen),
                                                                            new SqlParameter("@CMND", entity.CMND),
                                                                            new SqlParameter("@GioiTinh",
                                                                                             entity.GioiTinh),
                                                                            new SqlParameter("@NgaySinh",
                                                                                             entity.NgaySinh),
                                                                            new SqlParameter("@Email", entity.Email),
                                                                            new SqlParameter("@DienThoai",
                                                                                             entity.DienThoai),
                                                                            new SqlParameter("@IDTinhThanhPho",
                                                                                             entity.IDTinhThanhPho),
                                                                            new SqlParameter("@NgayDangKy",
                                                                                             entity.NgayDangKy),
                                                                            new SqlParameter("@NgayKetThuc",
                                                                                             entity.NgayKetThuc)
                                                                        }
                                                    });
        }

        public bool Remove(object id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spTaiKhoanDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDTaiKhoan", id)
                                                                        }
                                                    });
        }

        public List<TaiKhoan> GetTaiKhoan()
        {
            List<TaiKhoan> taiKhoan = new List<TaiKhoan>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from TaiKhoan",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                taiKhoan.Add(TaiKhoan.Convert(row));
            }
            return taiKhoan;
        }

        public TaiKhoan GetTaiKhoan(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from TaiKhoan where IDTaiKhoan = @IDTaiKhoan",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                            new SqlParameter("@IDTaiKhoan",id), 
                                                                         }
                                                     });
            return TaiKhoan.Convert(dtSet.Tables[0].Rows[0]);
        }
        
        public bool IsExists(string tenDangNhap)
        {
            bool bKT = false;
            DataSet dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                             {
                                                                 CommandText = "select * from TaiKhoan where TenDangNhap=@TenDangNhap",
                                                                 CommandType = CommandType.Text,
                                                                 Parameter = new []
                                                                                 {
                                                                                     new SqlParameter("@TenDangNhap",tenDangNhap) 
                                                                                 }
                                                             });
            if(dtSet.Tables[0].Rows.Count > 0)
            {
                bKT = true;
            }
            return bKT;
        }

        public bool IsAccount(string tenDangNhap, string matKhau)
        {
            bool bKT = false;
            DataSet dtSet = BusinessHelper.TruyVanDuLieu(
                new Command
                    {
                        CommandText = "select * from TaiKhoan where TenDangNhap =@TenDangNhap and MatKhau = @MatKhau",
                        CommandType = CommandType.Text,
                        Parameter = new[]
                                        {
                                            new SqlParameter("@TenDangNhap", tenDangNhap),
                                            new SqlParameter("MatKhau", matKhau)
                                        }
                    });
            if(dtSet.Tables[0].Rows.Count > 0)
            {
                bKT = true;
            }

            return bKT;
        }

        public TaiKhoan GetIDByTenDangNhap(string tenDangNhap)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
            {
                CommandText = "select * from TaiKhoan where TenDangNhap = @TenDangNhap",
                CommandType = CommandType.Text,
                Parameter = new[]
                                                                         {
                                                                            new SqlParameter("@TenDangNhap",tenDangNhap), 
                                                                         }
            });
            return TaiKhoan.Convert(dtSet.Tables[0].Rows[0]);
        }
    }
}
